import { IndustryInfoParams, IndustryInfoResponse } from 'admin/schema/common';
import { CorporateInfo } from 'admin/schema/Corporate';
import {
  GetDataPermissionRuleParams,
  DataPermissionRuleInfo,
  AddDataPermissionRuleParams,
  DataPermissionRuleDetailInfo,
  DataPermissionResource,
  DataPermissionRuleType,
  DataPermissionField,
  DataPermissionFieldDecisionSymbol,
  DataPermissionRuleUser,
  DataPermissionRuleRole,
  AddDataPermissionRuleDistributionParams,
  DelDataPermissionRuleDistributionParams,
  GetDataPermissionRuleUserParams,
  GetDataPermissionUserRules,
  DelDataPermissionRuleParam,
} from 'admin/schema/DataPermission';
import { OrganizationInfo } from 'admin/schema/Organization';
import { FetchPermissionResponse } from 'admin/schema/PermissionTypes';
import {
  RoleInfo,
  GetRoleListParams,
  PermissionRoleInfo,
  ModifyPermissionParams,
} from 'admin/schema/Role';
import {
  EditUserParams,
  GetUserInfoParams,
  GetConditionUserParams,
  UserInfo,
} from 'admin/schema/User';
import $http from '../../../servers/http';
import { Result } from '../../schema/Result';
/* 账户管理-企业管理
---------------------------------------------------------------- */
/**新增企业 */
export const createCorporate = (data: CorporateInfo): Promise<any> => {
  return $http.post({
    url: '/sso/company',
    data,
  });
};

/**删除企业 */
export const deleteCorporate = (id: number): Promise<Result<null>> => {
  return $http.delete({
    url: `/sso/company/${id}`,
  });
};

/**编辑企业 */
export const modifyCorporate = (id: number, data: CorporateInfo): Promise<Result<null>> => {
  return $http.put({
    url: `/sso/company/${id}`,
    data,
  });
};

/**查询单个企业详情 */
export const getCorporate = (id: number): Promise<Result<CorporateInfo>> => {
  return $http.get({ url: `/sso/company/${id}` });
};

/**查询企业列表 */
export const getCorporateList = (data: {
  pageSize: number;
  pageNo: number;
  companyName: string;
}): Promise<Result<CorporateInfo[]>> => {
  return $http.get({
    url: '/sso/company',
    data,
  });
};

/** 查询企业行业 */
export const getIndustryList = (
  data: IndustryInfoParams
): Promise<Result<IndustryInfoResponse[]>> => {
  return $http.get({
    url: '/sso/industry',
    data,
  });
};

/* 数据权限2.0
---------------------------------------------------------------- */
/**查询数据权限规则列表 */
export const getDataPermissionRules = (
  data: GetDataPermissionRuleParams
): Promise<Result<DataPermissionRuleInfo[]>> => {
  return $http.get({
    url: '/sso/datapermission/rules',
    data,
  });
};

/**新增数据权限规则 */
export const addDataPermissionRule = (data: AddDataPermissionRuleParams): Promise<Result<null>> => {
  return $http.post({
    url: '/sso/datapermission/rule',
    data,
  });
};

/** 删除数据权限规则 */
export const delDataPermissionRule = (
  ruleId: number,
  data: DelDataPermissionRuleParam
): Promise<Result<null>> => {
  return $http.delete({
    url: `/sso/datapermission/rule/${ruleId}`,
    data,
  });
};

/**修改数据权限规则 */
export const modifyDataPermissionRule = (
  ruleId: number,
  data: AddDataPermissionRuleParams
): Promise<Result<null>> => {
  return $http.put({
    url: `/sso/datapermission/rule/${ruleId}`,
    data,
  });
};

/**获取单个数据权限规则详情信息 */
export const getDataPermissionRule = (
  ruleId: number
): Promise<Result<DataPermissionRuleDetailInfo>> => {
  return $http.get({
    url: `/sso/datapermission/rule/${ruleId}`,
  });
};

/**查询数据权限数据源 */
export const getDataPermissionResources = (): Promise<Result<DataPermissionResource[]>> => {
  return $http.get({
    url: `/sso/datapermission/resources`,
  });
};

/**查询数据权限规则类型 */
export const getDataPermissionRuleTypes = (): Promise<Result<DataPermissionRuleType[]>> => {
  return $http.get({
    url: `/sso/datapermission/rule/types`,
  });
};

/**查询数据权限数据源属性 */
export const getDataPermissionResourceFields = (
  resourceKeys: string[]
): Promise<Result<DataPermissionField[]>> => {
  const params = resourceKeys.map(_ => `resourceKeys=${_}`).join('&');
  return $http.get({
    url: `/sso/datapermission/resource/fields?${params}`,
  });
};

/**查询数据源属性支持的判定符 */
export const getDataPermissionFieldDecisionSymbols = (
  fieldKey: string
): Promise<Result<DataPermissionFieldDecisionSymbol[]>> => {
  return $http.get({
    url: `/sso/datapermission/field/${fieldKey}/decisionSymbols`,
  });
};

/**查询数据权限规则关联用户列表 */
export const getDataPermissionRuleUsers = (
  ruleId: number,
  data: GetDataPermissionRuleUserParams
): Promise<Result<DataPermissionRuleUser[]>> => {
  return $http.get({
    url: `/sso/datapermission/rule/${ruleId}/users`,
    data,
  });
};

/**查询数据权限规则关联角色列表 */
export const getDataPermissionRuleRoles = (
  ruleId: number,
  data: GetDataPermissionRuleUserParams
): Promise<Result<DataPermissionRuleRole[]>> => {
  return $http.get({
    url: `/sso/datapermission/rule/${ruleId}/roles`,
    data,
  });
};

/**分配用户数据权限 */
export const addDataPermissionRuleDistribution = (
  data: AddDataPermissionRuleDistributionParams
): Promise<Result<DataPermissionResource>> => {
  return $http.post({
    url: `/sso/datapermission/rule/distribution`,
    data,
  });
};

/**取消用户数据权限 */
export const delDataPermissionRuleDistribution = (
  data: DelDataPermissionRuleDistributionParams
): Promise<Result<null>> => {
  return $http.delete({
    url: `/sso/datapermission/rule/distribution`,
    data,
  });
};

/**查询用户数据权限 */
export const getDataPermissionUserRules = (
  userId: number
): Promise<Result<GetDataPermissionUserRules[]>> => {
  return $http.get({
    url: `/sso/datapermission/user/${userId}/rules`,
  });
};

/* 用户管理
---------------------------------------------------------------- */
/**新增用户 */
export const addUser = (data: EditUserParams): Promise<Result<null>> => {
  return $http.post({
    url: '/sso/user',
    data,
  });
};

/** 删除用户 */
export const delUser = (userId: number): Promise<Result<null>> => {
  return $http.delete({
    url: `/sso/user/${userId}`,
  });
};

/**批量删除用户 */
export const mulDelUser = (data: number[]): Promise<Result<null>> => {
  return $http.delete({
    url: '/sso/user',
    data: { userIds: data },
  });
};

/**编辑用户 */
export const modifyUser = (id: number, data: EditUserParams): Promise<Result<null>> => {
  return $http.put({
    url: `/sso/user/${id}`,
    data,
  });
};

/**修改用户手机号 */
export const editUserPhone = (
  id: number,
  data: {
    mobile: string;
  }
): Promise<Result<null>> => {
  return $http.put({
    url: `/sso/user/${id}/mobile`,
    data,
  });
};

/**获取用户列表 */
export const getUserList = (data: GetUserInfoParams): Promise<Result<UserInfo[]>> => {
  return $http.get({
    url: '/sso/user',
    data,
  });
};

/**获取用户列表 数据管理员使用 */
export const getUserListWithDataPermissionUser = (
  data: GetUserInfoParams
): Promise<Result<UserInfo[]>> => {
  return $http.get({
    url: '/sso/user/datapermissionRule',
    data,
  });
};

/**
 * 条件查询用户列表
 * 和getUserList的区别在于此接口支持compangId的查询
 */
export const getConditionUser = (data: GetConditionUserParams): Promise<Result<UserInfo[]>> => {
  return $http.get({
    url: '/sso/user/datapermission',
    data,
  });
};

/**获取单个用户详细信息 */
export const getUserInfo = (userId: number): Promise<Result<UserInfo>> => {
  return $http.get({
    url: `/sso/user/${userId}`,
  });
};

/**重置密码 */
export const resetPwdRequest = (data: {
  userName: string;
  passWord: string;
  userId: number;
}): Promise<Result<null>> => {
  return $http.patch({
    url: '/sso/user/password/reset',
    data,
  });
};

/**停用/启用用户 */
export const enableUser = (userId: number, data: { userStatus: 0 | 1 }): Promise<Result<null>> => {
  return $http.patch({
    url: `/sso/user/${userId}`,
    data,
  });
};

/**用户账号延期 */
export const delayUser = (userId: number, data: { date: string }): Promise<Result<null>> => {
  return $http.post({
    url: `/sso/user/${userId}/delay`,
    data,
  });
};
/**解除用户异常状态 */
export const relieveUserStatus = (userId: number): Promise<Result<null>> => {
  return $http.post({
    url: `/sso/user/${userId}/cancelAccount`,
  });
};
/* 角色管理
---------------------------------------------------------------- */
/**新增角色信息 */
export const addRole = (data: RoleInfo): Promise<Result<null>> => {
  return $http.post({
    url: '/sso/role',
    data,
  });
};

/**查询角色信息 */
export const getRoleInfo = (roleId: number): Promise<Result<RoleInfo>> => {
  return $http.get({
    url: `/sso/role/${roleId}`,
  });
};

/**删除角色信息 */
export const delRole = (roleId: number): Promise<Result<null>> => {
  return $http.delete({
    url: `/sso/role/${roleId}`,
  });
};

/**编辑角色信息 */
export const modifyRole = (data: RoleInfo): Promise<Result<null>> => {
  return $http.put({
    url: `/sso/role/${data.roleId}`,
    data,
  });
};

/**获取角色列表 */
export const getRoleList = (data: GetRoleListParams): Promise<Result<RoleInfo[]>> => {
  return $http.get({
    url: '/sso/role',
    data,
  });
};

/**获取角色列表 无数据权限 */
export const getRoleListNoPermission = (data: GetRoleListParams): Promise<Result<RoleInfo[]>> => {
  return $http.get({
    url: '/sso/role/datapermissionRule',
    data,
  });
};

/**数据权限下需要的角色列表查询 */
export const getPermissionRole = (
  data: GetRoleListParams
): Promise<Result<PermissionRoleInfo[]>> => {
  return $http.get({
    url: '/sso/role/datapermission',
    data,
  });
};

/**查询菜单功能权限 */
export const getMenuPermission = (): Promise<FetchPermissionResponse> => {
  return $http.get({
    url: `/sso/menu`,
  });
};

/**查询角色功能权限 */
export const getRolePermission = (roleId: number): Promise<FetchPermissionResponse> => {
  return $http.get({
    url: `/sso/role/permissions/${roleId}`,
  });
};

/**保存角色功能权限 */
export const modifyRolePermission = (data: ModifyPermissionParams): Promise<Result<null>> => {
  return $http.put({
    url: `/sso/role/permissions/${data.roleId}`,
    data,
  });
};

/**根据组织查角色 */
export const getRoleByOrganization = (
  organizationId: number
): Promise<Result<{ roleName: string; roleId: number }[]>> => {
  return $http.get({
    url: `/sso/organization/${organizationId}/roles`,
  });
};

/* 组织机构管理
---------------------------------------------------------------- */
/**新增组织机构 */
export const addOrganization = (
  id: number,
  organizationName: string
): Promise<Result<{ organizationId: number }>> => {
  return $http.post({
    url: '/sso/organization',
    data: {
      idParent: id,
      organizationName: organizationName,
    },
  });
};

/**删除组织机构 */
export const delOrganization = (id: number): Promise<Result<null>> => {
  return $http.delete({
    url: `/sso/organization/${id}`,
  });
};

/**编辑组织机构 */
export const modifyOrganization = (id: number, organizationName: string): Promise<Result<null>> => {
  return $http.put({
    url: `/sso/organization/${id}`,
    data: {
      organizationName: organizationName,
    },
  });
};

/**查询组织机构 */
export const getOrganization = (): Promise<Result<OrganizationInfo[]>> => {
  return $http.get({
    url: '/sso/organization',
  });
};

/**查询当前登录用户企业组织树*/
export const getOrganizationNoPermission = (): Promise<Result<OrganizationInfo[]>> => {
  return $http.get({
    url: '/sso/organization/datapermissionRule',
  });
};

/**
 * 根据公司id查询组织机构
 */
export const getOrganizationByCompany = (id: number): Promise<Result<OrganizationInfo[]>> => {
  return $http.get({
    url: `/sso/organization/condition?companyId=${id}`,
  });
};

/**
 * 查询用户列表-无数据权限
 */
export const getUsersWithoutPermission = (data: {
  userName: string;
  pageNo: number;
  pageSize: number;
}): Promise<Result<UserInfo[]>> => {
  return $http.get({
    url: `/sso/user/canceldatapermission`,
    data,
  });
};
